package tec.stan.ling.topo.tree;

import java.lang.reflect.InvocationTargetException;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.ProgressMonitorDialog;
import org.eclipse.jface.operation.IRunnableWithProgress;
import org.eclipse.jface.viewers.DoubleClickEvent;
import org.eclipse.jface.viewers.IDoubleClickListener;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.part.ViewPart;

import tec.stan.ling.core.CorePlugin;
import tec.stan.ling.core.service.ServiceManager;
import tec.stan.ling.core.top.model.NE;
import tec.stan.ling.core.top.model.Subnet;
import tec.stan.ling.topo.graph.TopoEditInput;
import tec.stan.ling.topo.graph.TopoViewEditor;

public class ElementTreeView extends ViewPart {

	private static final org.apache.log4j.Logger log = org.apache.log4j.Logger
			.getLogger(ElementTreeView.class);

	public static final String ID = "tec.stan.ling.topo.tree";

	private TreeViewer viewer;

	public ElementTreeView() {
	}

	@Override
	public void createPartControl(Composite parent) {
		ServiceManager manager = CorePlugin.getDefault().getServiceManager();
		Subnet root = manager.getTopoService().getRoot();
		// Root root = ElementUtil.createDemo();

		viewer = new TreeViewer(parent, SWT.MULTI | SWT.H_SCROLL | SWT.V_SCROLL);
		ElementTreeProvider provider = new ElementTreeProvider();
		viewer.setContentProvider(provider);
		viewer.setLabelProvider(provider);
		// viewer.setSorter(new ViewerSorter());
		viewer.setInput(root);

		// makeActions();
		// addListener();
		// hookContextMenu();
		hookDoubleClickAction();
		// contributeToActionBars();
		// initDragAndDrop();
	}

	@Override
	public void setFocus() {

	}

	private void hookDoubleClickAction() {
		viewer.addDoubleClickListener(new IDoubleClickListener() {
			public void doubleClick(DoubleClickEvent event) {

				IStructuredSelection selection = (IStructuredSelection) viewer
						.getSelection();
				if (!selection.isEmpty()) {
					Object element = selection.getFirstElement();
					if (element instanceof NE) {
						openEditor((NE) element);
					}
				}

			}
		});
	}

	private void openEditor(final NE element) {
		IRunnableWithProgress op = new IRunnableWithProgress() {
			public void run(IProgressMonitor monitor)
					throws InvocationTargetException, InterruptedException {
				Display.getDefault().asyncExec(new Runnable() {
					public void run() {
						try {
							log.debug("open Topo View Editor start");
							IEditorInput input = new TopoEditInput(element);
							getViewSite().getPage().openEditor(input,
									TopoViewEditor.ID, true);
							log.debug("open Topo View Editor end");
						} catch (Exception e) {
							log.debug(e.getMessage(), e);
						}
					}
				});
			}
		};
		try {
			new ProgressMonitorDialog(Display.getCurrent().getActiveShell())
					.run(true, false, op);
		} catch (Exception e) {
			log.debug(e.getMessage(), e);
		}
	}
}
